package com.bocai.eelly.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.activitys.ChoosePhotosActivity;
import com.bocai.eelly.adapter.DialogAdapter;
import com.bocai.eelly.bean.DialogBean;
import com.bocai.eelly.common.BaseFragment;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.ApiManage;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.DateTimePickDialogUtil;
import com.bocai.eelly.utils.DialogUtil;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.TimeUtils;
import com.bocai.eelly.utils.Utils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * 发布库存面料
 */
public class PostMianliaoStockFragment extends BaseFragment implements View.OnClickListener {
    @Bind(R.id.tv_post_fabric_stock_type)
    TextView tvPostFabricStockType;
    @Bind(R.id.ll_post_fabric_stock_type)
    LinearLayout llPostFabricStockType;
    @Bind(R.id.et_post_fabric_stock_name)
    EditText etPostFabricStockName;
    @Bind(R.id.et_post_fabric_stock_processing)
    EditText etPostFabricStockProcessing;
    @Bind(R.id.et_post_fabric_stock_fabric)
    EditText etPostFabricStockFabric;
    @Bind(R.id.et_post_fabric_stock_color)
    EditText etPostFabricStockColor;
    @Bind(R.id.et_post_fabric_stock_weight)
    EditText etPostFabricStockWeight;
    @Bind(R.id.tv_post_fabric_stock_weight)
    TextView tvPostFabricStockWeight;
    @Bind(R.id.et_post_fabric_stock_width)
    EditText etPostFabricStockWidth;
    @Bind(R.id.tv_post_fabric_stock_width)
    TextView tvPostFabricStockWidth;
    @Bind(R.id.et_post_fabric_stock_num)
    EditText etPostFabricStockNum;
    @Bind(R.id.et_post_fabric_stock_price)
    EditText etPostFabricStockPrice;
    @Bind(R.id.tv_post_fabric_stock_date)
    TextView tvPostFabricStockDate;
    @Bind(R.id.et_post_fabric_stock_backup)
    EditText etPostFabricStockBackup;
    @Bind(R.id.ll_post_fabric_stock_choose_photo)
    LinearLayout llPostFabricStockChoosePhoto;
    @Bind(R.id.tv_post_fabric_stock_commit)
    TextView tvPostFabricStockCommit;
    @Bind(R.id.ll_post_fabric_stock_date)
    LinearLayout llPostFabricStockDate;
    private View view;

    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_post_mianliao_stock, null);
        initView();
        ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }

    private void initView() {
        ButterKnife.bind(this, view);
        initEvent();
    }

    private void initEvent() {
        llPostFabricStockChoosePhoto.setOnClickListener(this);
        llPostFabricStockType.setOnClickListener(this);
        llPostFabricStockDate.setOnClickListener(this);
        tvPostFabricStockCommit.setOnClickListener(this);
        tvPostFabricStockWeight.setOnClickListener(this);
        tvPostFabricStockWidth.setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.ll_post_fabric_stock_choose_photo:
                Intent intent=new Intent(getActivity(),ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.ll_post_fabric_stock_date:
                new DateTimePickDialogUtil(getActivity()).dateTimePicKDialog(tvPostFabricStockDate);
                break;
            case R.id.ll_post_fabric_stock_type:
                String timeline = Utils.getTimeline();
                String sign = Utils.md5(timeline);
                ApiManage.getFabricStockType(this, timeline, sign, Utils.getUserId(getActivity(), "id"), new Action1<DialogBean>() {
                    @Override
                    public void call(DialogBean dialogBean) {
                        if (Utils.checkResultCode(dialogBean.getReturn_code())) {
                            List<String> titles = new ArrayList<>();
                            for (int i = 0; i <dialogBean.getData().size(); i++) {
                                titles.add(dialogBean.getData().get(i).getTitle());
                            }
                            DialogAdapter dialogAdapter = new DialogAdapter(getActivity(), titles);
                            DialogUtil.showDialog(getActivity(), dialogAdapter, new OnItemClickListener() {
                                @Override
                                public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
//                                    T.showShort(getActivity(), item.toString());
                                    tvPostFabricStockType.setText(item.toString());
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
                break;
            case R.id.tv_post_fabric_stock_weight:
                List<String> wights = new ArrayList<>();
                String[] wight = {"克/平方米", "克/平方码","姆米"};
                for (int i = 0; i < wight.length; i++) {
                    wights.add(wight[i]);
                }
                DialogUtil.showDialog(getActivity(), new DialogAdapter(getActivity(), wights), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFabricStockWeight.setText(item.toString());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_post_fabric_stock_width:
                List<String> nums = new ArrayList<>();
                String[] num = {"厘米", "英吋"};
                for (int i = 0; i < num.length; i++) {
                    nums.add(num[i]);
                }
                DialogUtil.showDialog(getActivity(), new DialogAdapter(getActivity(), nums), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFabricStockWidth.setText(item.toString());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_post_fabric_stock_commit:

                if (files == null || files.length == 0) {
                    T.showShort(getActivity(), "请选择照片");
                } else {
                    String type = tvPostFabricStockType.getText().toString();
                    String name=etPostFabricStockName.getText().toString();
                    String processing = etPostFabricStockProcessing.getText().toString();
                    String fabric = etPostFabricStockFabric.getText().toString();
                    String color = etPostFabricStockColor.getText().toString();
                    String weight = etPostFabricStockWeight.getText().toString();
                    String width = etPostFabricStockWidth.getText().toString();
                    String numname = etPostFabricStockNum.getText().toString();
                    String price = etPostFabricStockPrice.getText().toString();
                    String date = tvPostFabricStockDate.getText().toString();
                    String backup = etPostFabricStockBackup.getText().toString();
                    if (Utils.isEmpty(type)) {
                        T.showShort(getActivity(), "请选择面料类别");
                    } else if (Utils.isEmpty(name)) {
                        T.showShort(getActivity(), "请输入面料名称");
                    } else if (Utils.isEmpty(processing)) {
                        T.showShort(getActivity(), "请输入加工工艺");
                    } else if (Utils.isEmpty(fabric)) {
                        T.showShort(getActivity(), "请输入面料成分");
                    } else if (Utils.isEmpty(color)) {
                        T.showShort(getActivity(), "请输入颜色名称");
                    } else if (Utils.isEmpty(weight)) {
                        T.showShort(getActivity(), "请输入克重");
                    }else if (Utils.isEmpty(width)) {
                        T.showShort(getActivity(), "请输入门幅数值");
                    } else if (Utils.isEmpty(numname)) {
                        T.showShort(getActivity(), "请输入数量");
                    }else if (Utils.isEmpty(price)) {
                        T.showShort(getActivity(), "请选择目标价格");
                    } else if (Utils.isEmpty(date)) {
                        T.showShort(getActivity(), "请输入生产年份");
                    } else if (Utils.isEmpty(backup)) {
                        T.showShort(getActivity(), "请输入其他说明");
                    } else {
                        Loading.show(getActivity(), getString(R.string.str_loading));
                        commit();
                    }
                }
                break;
        }
    }
    private void commit() {
        String buildDate= TimeUtils.dataOne(tvPostFabricStockDate.getText().toString());
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POST_FABRIC_STOCK, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(getActivity(),e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(getActivity(), response);
                }
            },files,fileKeys,getParams(etPostFabricStockName.getText().toString(),tvPostFabricStockType.getText().toString(),etPostFabricStockFabric.getText().toString(),
                    etPostFabricStockProcessing.getText().toString(),etPostFabricStockColor.getText().toString(),
                    etPostFabricStockWeight.getText().toString(),tvPostFabricStockWeight.getText().toString(),etPostFabricStockWidth.getText().toString(),
                    tvPostFabricStockWidth.getText().toString(),etPostFabricStockNum.getText().toString(),etPostFabricStockPrice.getText().toString(),
                    buildDate,etPostFabricStockBackup.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public OkHttpClientManager.Param[] getParams(String title, String kind, String fabric, String processing, String color, String weight,
                                                 String weightunit, String  width, String widthunit,String num,String price,
                                                 String builddate,String backup){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(getActivity(),"id")),
                new OkHttpClientManager.Param("title", title),
                new OkHttpClientManager.Param("kind", kind),
                new OkHttpClientManager.Param("fabric", fabric),
                new OkHttpClientManager.Param("processing", processing),
                new OkHttpClientManager.Param("color", color),
                new OkHttpClientManager.Param("weight", weight),
                new OkHttpClientManager.Param("weightunit", weightunit),
                new OkHttpClientManager.Param("width", width),
                new OkHttpClientManager.Param("widthunit", widthunit),
                new OkHttpClientManager.Param("num", num),
                new OkHttpClientManager.Param("price", price),
                new OkHttpClientManager.Param("builddate", builddate),
                new OkHttpClientManager.Param("backup", backup)
        };
    }
}
